﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    //工厂模式:模拟现实社会中制造产品的工厂，常常用于对对象复杂的构造、实例化、初始化过程进行封装；
    //将工厂的制造方法进行规范化，允许子类根据具体制造的产品类实现多态；降低系统的耦合，使系统的可维护性、可扩展性等得到提高，这样就将工厂模式升级为了一种设计模式
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Enemy
        {
            public Enemy()
            {
               
            }
        }

        public interface IFactory
        {
            Enemy Create();
        }
        //对对象复杂的构造、实例化、初始化进行封装。
        #region Tank
        public class Tank : Enemy
        {
            /// <summary>
            /// 名字
            /// </summary>
             public string Name { set; get; }
            /// <summary>
            /// 使用年限
            /// </summary>
            public int UsefulLife { set; get; }
            /// <summary>
            /// 最大使用年限
            /// </summary>
            public int MaxLife { set; get; }
            /// <summary>
            /// 颜色
            /// </summary>
            public Color Color { set; get; }

            /// <summary>
            /// 加油
            /// </summary>
            public void Refuel()
            {

            }
            /// <summary>
            /// 刷新仪器计时
            /// </summary>
            public void RefreshTime()
            {

            }
            /// <summary>
            /// 装配弹药
            /// </summary>
            public void LoadAmmo()
            {

            }
            /// <summary>
            /// 维护
            /// </summary>
            public void Maintain()
            {

            }
            /// <summary>
            /// 检查胎压
            /// </summary>
            public void CheckTirePressure(int i)
            {

            }

        }
        public class TankFactory : IFactory
        {
            public Enemy Create()
            {
                #region 模拟复杂的构造、实例化、初始化过程
                #region 构造和实例化
                //准备Tank的组装部件等等
                Tank tank = new Tank();
                tank.Name = "虎式坦克001";
                tank.UsefulLife = 2;
                tank.UsefulLife = 5;
                tank.Color = Color.Green;
                #endregion
                #region 初始化
                tank.Refuel();
                tank.RefreshTime();
                tank.Maintain();
                for (int i = 0; i < 4; i++)
                {
                    tank.CheckTirePressure(i);
                }
                #endregion 
                #endregion
                return tank;
            }
        }
        #endregion

        //可扩展性好：每次添加一个类时，创建类的本身和其对应的生产工厂类即可
        #region Boss
        public class Boss : Enemy
        {
            public int Age { set; get; }
            public string Name { set; get; }
            public int Level { set; get; }
            public string Skill { set; get; }
        }

        public class BossFactory : IFactory
        {
            public Enemy Create()
            {
  
                Boss boss = new Boss();
                boss.Age = 1111;
                boss.Level = 100;
                boss.Skill = "地爆天星";
                boss.Name = "哥布林"+DateTime.Now.Ticks;
                return boss;
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            IFactory factory = new TankFactory();
            Enemy tank1 = factory.Create();

            factory = new BossFactory();
            Enemy boss1 = factory.Create();
        }
    }
}
